import { useState, useCallback } from 'react';
import { UI_CONFIG } from '../config/constants';

export const useInput = (onSend) => {
    const [inputValue, setInputValue] = useState('');

    const handleInputChange = useCallback((value) => {
        if (value.length <= UI_CONFIG.MAX_MESSAGE_LENGTH) {
            setInputValue(value);
        }
    }, []);

    const handleSend = useCallback(() => {
        if (inputValue.trim()) {
            onSend(inputValue.trim());
            setInputValue('');
        }
    }, [inputValue, onSend]);

    const resetInput = useCallback(() => {
        setInputValue('');
    }, []);

    const handleKeyDown = useCallback((e) => {
        if (e.key === 'Enter' && !e.shiftKey) {
            e.preventDefault();
            handleSend();
        }
    }, [handleSend]);

    return {
        inputValue,
        setInputValue: handleInputChange,
        handleSend,
        handleKeyDown,
        resetInput,
    };
};